//
//  FingerChineseViewController.m
//  FingerChinese
//
//  Created by luar on 20/01/2010.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "FingerChineseViewController.h"
#import "UIPageScrollView.h"
#import "HitArea.h"
#import "VoiceCoord.h"
#import "PageView.h"

@implementation FingerChineseViewController

@synthesize photoA;

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	flashCardPageNo = -1;
	isFingerPage = YES;
	photoA = [[NSMutableArray array] retain];
	return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	NSString *file = [photoA objectAtIndex: 0];
	UIImageView *img = [[UIImageView alloc] initWithImage: 
						[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", file]]];
	
	_scrollView.frameWidth = img.frame.size.width;
	_scrollView.frameHeight = img.frame.size.height;
	_scrollView.pageQuantity = [photoA count];	
	[_scrollView initPage];
	
	[img release];
	
	if (initPageNo > -1) {
		[self jumpPage:initPageNo];	
	} else {
		[self jumpPageDid:1 pageQuantity:_scrollView.pageQuantity];	
	}
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJumpPage:) name:@"JumpPage" object:nil];
}

# pragma mark -
# pragma mark UIPageScrollView

- (PageView*)genPage:(NSInteger)pageIndex isNewPage:(BOOL)newPage {
	
	NSString *file = [photoA objectAtIndex: pageIndex];
	
	UIImageView *img = [[UIImageView alloc] initWithImage: 
						[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", file]]];
	
	PageView *_page = [_scrollView genPage:pageIndex isNewPage:newPage];
	NSLog(@"FCVC page %f %f", _page.center.x, _page.center.y);
	voiceCoord = [[NSMutableArray alloc] init];
	NSString *htmPath = [[NSBundle mainBundle]
						 pathForResource:file
						 ofType:@"htm"
						 inDirectory:@"/"];
	//NSLog(@"htmPath: %@\n", htmPath);
	NSURL *url = [[NSURL alloc] initFileURLWithPath:htmPath];
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	[xmlParser setDelegate:self];
	[xmlParser parse];
	[url release];
	[xmlParser setDelegate:nil];
	[xmlParser release];
	
	UIView *hotspot = [[[UIView alloc] initWithFrame:img.frame] autorelease];
	
	NSUInteger len = [voiceCoord count];
	for (int i=0;i<len;i++) {
		VoiceCoord *vc = [voiceCoord objectAtIndex:i];
		HitArea *ha =  [[[HitArea alloc] initWithFrame:CGRectMake(vc.x, vc.y, vc.w, vc.h)] autorelease];
		NSRange range = [vc.mp3Path rangeOfString:@"age:"];
		if (range.location == NSNotFound) {
			NSString *mp3Path = [[NSBundle mainBundle]
								 pathForResource:vc.mp3Path
								 ofType:@"mp3"
								 inDirectory:@"/"];
			ha.mp3Path = mp3Path;
			ha.pageNo = 0;
		} else {
			int _pageNo = [[vc.mp3Path substringFromIndex:5] intValue];
			ha.pageNo = _pageNo;
		}
		[hotspot addSubview:ha];
		//[ha release];
	}
	[_page addSubview:img];
	[_page addSubview:hotspot];

	[img release];
	//[hotspot release];
	[voiceCoord release];
	//NSLog(@"FCVC genPage %i %i %f %f", pageIndex, newPage, _page.frame.size.width, img.frame.size.height);
	return _page;
}

- (void)onJumpPage:(NSNotification *)evt {
	HitArea* ha = (HitArea*) [evt object];
	[_scrollView jumpPage:ha.pageNo-1];
}

#pragma mark -

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if([elementName isEqualToString:@"area"]) {
		VoiceCoord *vc = [[VoiceCoord alloc] init];
		vc.mp3Path = [attributeDict objectForKey:@"href"];
		NSArray *coords = [[attributeDict objectForKey:@"coords"] componentsSeparatedByString:@","];
		vc.x = [[coords objectAtIndex: 0] intValue];
		vc.y = [[coords objectAtIndex: 1] intValue];
		vc.w = [[coords objectAtIndex: 2] intValue]-[[coords objectAtIndex: 0] intValue];
		vc.h = [[coords objectAtIndex: 3] intValue]-[[coords objectAtIndex: 1] intValue];
		[voiceCoord  addObject:vc];
		[vc release];
	}
}

#pragma mark -

- (void)dealloc {
	NSLog(@"FCVC dealloc");
	[picker release];
	[photoA release];
	_scrollView.delegate = nil;
	[_scrollView release];
    [super dealloc];
}

@end
